package com.jiantong.user.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Code generated by CodeGen
 * Desc: 用户扩展信息表Model类
 * Date: 2017-03-14
 */
@Data
public class OrganizationUser implements Serializable {

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -6243162263363116101L;

    private Long id;
    
    /**
     * 用户id
     */
    private Long userId;
    
    /**
     * 用户名
     */
    private String userName;
    
    /**
     * 组织机构ID
     */
    private Long organizationId;
    
    /**
     * 组织机构名
     */
    private String organizationName;
    
    /**
     * 机构类型 1、养殖场 2、屠宰场 3、政府
     */
    private Integer organizationType;
    
    /**
     * 角色类型
     */
    private Integer roleType;
    
    /**
     * 角色名称
     */
    private String roleName;

    private Map<String , Map<String ,String>> corporateCharterMap;

    @JsonIgnore
    private String corporateCharter;
    
    private Date createdAt;
    
    private Date updatedAt;


    public void setCorporateCharter(String corporateCharter){
        this.corporateCharter = corporateCharter;

        if(Strings.isNullOrEmpty(corporateCharter)){
            this.corporateCharterMap = Maps.newHashMap();
        }else {
            try {
                this.corporateCharterMap = objectMapper.readValue(corporateCharter, new TypeReference<Map<String , Map<String , String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setCorporateCharterMap(Map<String , Map<String , String>> corporateCharterMap){
        this.corporateCharterMap = corporateCharterMap;

        if(corporateCharterMap == null || corporateCharterMap.isEmpty()){
            this.corporateCharter = null;
        }else {
            try {
                this.corporateCharter = objectMapper.writeValueAsString(corporateCharterMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }
}
